{
    "cells": [
        {
            "cell_type": "markdown",
            "id": "307804a3-c02b-4a57-ac0d-172c30ddc851",
            "metadata": {},
            "source": [
                "# Faiss Index Demo"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "f7010b1d-d1bb-4f08-9309-a328bb4ea396",
            "metadata": {},
            "source": [
                "#### Creating a Faiss Index"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "a1b5e530",
            "metadata": {},
            "outputs": [],
            "source": [
                "import logging\n",
                "import sys\n",
                "\n",
                "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
                "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 3,
            "id": "0c9f4d21-145a-401e-95ff-ccb259e8ef84",
            "metadata": {},
            "outputs": [],
            "source": [
                "import faiss\n",
                "# dimensions of text-ada-embedding-002\n",
                "d = 1536 \n",
                "faiss_index = faiss.IndexFlatL2(d)"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "8ee4473a-094f-4d0a-a825-e1213db07240",
            "metadata": {},
            "source": [
                "#### Load documents, build the GPTFaissIndex"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 4,
            "id": "0a2bcc07",
            "metadata": {},
            "outputs": [],
            "source": [
                "from gpt_index import GPTFaissIndex, SimpleDirectoryReader\n",
                "from IPython.display import Markdown, display"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 5,
            "id": "68cbd239-880e-41a3-98d8-dbb3fab55431",
            "metadata": {},
            "outputs": [],
            "source": [
                "# load documents\n",
                "documents = SimpleDirectoryReader('../paul_graham_essay/data').load_data()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "ba1558b3",
            "metadata": {},
            "outputs": [],
            "source": [
                "index = GPTFaissIndex.from_documents(documents, faiss_index=faiss_index)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 10,
            "id": "c36cadc1",
            "metadata": {},
            "outputs": [],
            "source": [
                "# save index to disk\n",
                "index.save_to_disk(\n",
                "    'index_faiss.json', \n",
                "    faiss_index_save_path=\"index_faiss_core.index\"\n",
                ")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 11,
            "id": "70b372a7",
            "metadata": {},
            "outputs": [],
            "source": [
                "# load index from disk\n",
                "index = GPTFaissIndex.load_from_disk(\n",
                "    'index_faiss.json', \n",
                "    faiss_index_save_path=\"index_faiss_core.index\"\n",
                ")"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "04304299-fc3e-40a0-8600-f50c3292767e",
            "metadata": {},
            "source": [
                "#### Query Index"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "35369eda",
            "metadata": {},
            "outputs": [],
            "source": [
                "# set Logging to DEBUG for more detailed outputs\n",
                "response = index.query(\"What did the author do growing up?\")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 13,
            "id": "bedbb693-725f-478f-be26-fa7180ea38b2",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/markdown": [
                            "<b>\n",
                            "The author grew up writing short stories, programming on an IBM 1401, and working on microcomputers. He wrote simple games, a program to predict how high his model rockets would fly, and a word processor. He studied philosophy in college, but switched to AI. He reverse-engineered SHRDLU for his undergraduate thesis and wrote a book about Lisp hacking. He visited the Carnegie Institute and realized he could make art that would last. He took art classes at Harvard and applied to RISD and the Accademia di Belli Arti in Florence.</b>"
                        ],
                        "text/plain": [
                            "<IPython.core.display.Markdown object>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "display(Markdown(f\"<b>{response}</b>\"))"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 7,
            "id": "99212d33",
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "> Starting query: What did the author do after his time at Y Combinator?\n",
                        "> Top 1 nodes:\n",
                        "> [Node 4] I don't think it was entirely luck that the first batch was so good. You had to be pretty bold to...\n",
                        "> Searching in chunk: I don't think it was entirely luck that the fir...\n",
                        "> Initial response: \n",
                        "The author started painting, then writing essays, and then working on Lisp again.\n",
                        "> [query] Total token usage: 3219 tokens\n"
                    ]
                }
            ],
            "source": [
                "# set Logging to DEBUG for more detailed outputs\n",
                "response = index.query(\"What did the author do after his time at Y Combinator?\")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 8,
            "id": "1a720ad6",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/markdown": [
                            "<b>\n",
                            "The author started painting, then writing essays, and then working on Lisp again.</b>"
                        ],
                        "text/plain": [
                            "<IPython.core.display.Markdown object>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "display(Markdown(f\"<b>{response}</b>\"))"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "b0b6d770",
            "metadata": {},
            "outputs": [],
            "source": []
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "Python 3 (ipykernel)",
            "language": "python",
            "name": "python3"
        },
        "language_info": {
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.11.1"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 5
}